package com.amado.course45mysql02.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 数据库连接
 */
public class DBConnection {
    private static String user = "student";
    private static String password = "student123";
    /**
     * 驱动
     */
    private static String className = "com.mysql.jdbc.Driver";
    //	private String className = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private static String url = "jdbc:mysql://47.98.129.170:3306/student?useUnicode=true&characterEncoding=utf8";


    public static Connection getConn() {
            try {
                Class.forName(className);
                return DriverManager.getConnection(url, user, password);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }

        return null;
    }

    // 关闭数据库
    public static void close(ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            state = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }

    // 测试数据库连接是否成功
    public static void main(String[] args) {
        System.out.println(getConn());
    }
}